package com.carlinfo.userscenter.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacUtils;

public class EncryptUtil
{
	private RegexUtil regexUtil = new RegexUtil();

	public String encodeStr(String souStr)
	{
		String encodeName = "sha256";
		String randStr = this.regexUtil.randString(32);
		String souEncode = randStr + souStr;
		String encodeStr = DigestUtils.sha256Hex(souEncode);
		String result = encodeName + "$" + randStr + "$" + encodeStr;
		return result;
	}

	public boolean checkStr(String souStr, String encodeStr)
	{
		String[] encodeStrs = encodeStr.split("\\$");
		if (encodeStrs.length != 3)
		{
			return false;
		}
		String encodeName = encodeStrs[0];
		String randStr = encodeStrs[1];
		String souEncode = randStr + souStr;
		String encodeRes = DigestUtils.sha256Hex(souEncode);
		String result = encodeName + "$" + randStr + "$" + encodeRes;
		return encodeStr.equalsIgnoreCase(result);
	}

	public static void main(String[] args)
	{
		testenc();
		EncryptUtil encryptUtil = new EncryptUtil();
		String souStr = "111111";
		String encodeStr = encryptUtil.encodeStr(souStr);
		System.out.println("==encodeStr===" + encodeStr);

		boolean flag = encryptUtil.checkStr("222", encodeStr);
		System.out.println("--checkStr-->" + flag);
	}

	private static void testenc()
	{
		String souStr = "111111";
		String md5Str = DigestUtils.md5Hex(souStr);
		System.out.println("==md5Str==" + md5Str + "--->length:" + md5Str.length());

		String sha256Str = DigestUtils.sha256Hex(souStr);
		System.out.println("==sha256Str==" + sha256Str + "--->length:" + sha256Str.length());

		String sha512Str = DigestUtils.sha512Hex(souStr);
		System.out.println("==sha512Str==" + sha512Str + "--->length:" + sha512Str.length());

		try
		{
			byte[] souceByte = souStr.getBytes("UTF-8");
			byte[] encodeByte = Base64.encodeBase64(souceByte);

			System.out.println("Base64==souceByte==" + Arrays.toString(souceByte));
			System.out.println("Base64==encodeByte==" + Arrays.toString(encodeByte));

			String encodeStr = new String(encodeByte);
			System.out.println("Base64==encodeStr==" + encodeStr);

			byte[] decodeByte = Base64.decodeBase64(encodeByte);
			System.out.println("Base64==decodeByte==" + Arrays.toString(decodeByte));

			String decodeStr = new String(decodeByte);
			System.out.println("Base64==decodeStr==" + decodeStr);

		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}

		String url = "http://www.baidu.com?id=1&a=10&b=20";
		try
		{
			String encodeUrl = URLEncoder.encode(url, "UTF-8");
			System.out.println("==URLEncoder===" + encodeUrl);

			String decodeUrl = URLDecoder.decode(encodeUrl, "UTF-8");
			System.out.println("==decodeUrl===" + decodeUrl);
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		String key = "111111";
		String value = "test";
		String result = HmacUtils.hmacSha256Hex(key, value);
		System.out.println(key + "==hmacSha256Hex====>" + result);
	}
}
